package ophiux.wechat.template.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import ophiux.OphiuxWechatTemplateMessageException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLUtil
{
	/**
	 * 
	 * @param inputstream 
	 * @return  Document
	 * @throws OphiuxWechatTemplateMessageException 
	 */
	public static Document buildDocument(InputStream inputstream) throws OphiuxWechatTemplateMessageException{
		Document document = null;
		try {
			DocumentBuilder documentBuilder = 
					DocumentBuilderFactory.newInstance().newDocumentBuilder();
 
			try {
				document = documentBuilder.parse(inputstream);
			} catch (SAXException e) {
				throw new OphiuxWechatTemplateMessageException("document parse error",e);
			} catch (IOException e) {
				throw new OphiuxWechatTemplateMessageException("file read error",e);
			}
		} catch (ParserConfigurationException e) {
			throw new OphiuxWechatTemplateMessageException("ParserConfigurationException",e);
		}
		return document;
	}
	
	/**
	 * ���xml�ַ�ת������ȡelement
	 * @param xmlStr   xmlԴ�ַ�
	 * @return
	 * @throws OphiuxWechatTemplateMessageException 
	 */
	public static Element getDocmentElement(String xmlStr) throws OphiuxWechatTemplateMessageException{
		return  buildDocument(getInputStream(xmlStr)).getDocumentElement();
	}
	
	
	/**
	 * ���xml�ַ����inputStream
	 * @param xmlStr
	 * @return
	 * @throws OphiuxWechatTemplateMessageException 
	 */
	public static InputStream getInputStream(String xmlStr) throws OphiuxWechatTemplateMessageException{
		InputStream is = null;
		try {
			is = new ByteArrayInputStream(xmlStr.getBytes("utf-8"));
		} catch (UnsupportedEncodingException e) {
			throw new OphiuxWechatTemplateMessageException("ParserConfigurationException",e);
		}
		return is;
	}
	
	/**
	 * ���xml��ǩ��ƻ�ȡ����element
	 * @param parent  ����element
	 * @param tagName ��ǩ��
	 * @return List<Element> �����б�
	 */
	public static List<Element> getChildElements(Element parent,String tagName){
		List<Element> childelementList = new ArrayList<Element>();
		//��ݱ�ǩ���ȡ���е�����
		NodeList nodes = parent.getElementsByTagName(tagName);
		//ѭ���ӽڵ��ȡ��Ӧ�ڵ��element
		for(int i = 0;i<nodes.getLength();i++){
			Node node = nodes.item(i);
			Element element = (Element) node;
			childelementList.add(element);
		}
		return childelementList;
	}
	
	/**
	 * ��ݱ�ǩ���ȡelement�ڵ�
	 * @param parent  ���ڵ�
	 * @param tagName ��ǩ��
	 * @return Element
	 */
	public static Element getChildElement(Element parent,String tagName){
		Element element = null;
		List<Element> childElementList = getChildElements(parent, tagName);
		if(ListUtil.isNotBlank(childElementList)){
			element = childElementList.get(0);
		}
		return element;
	}
	
	/**
	 * ��ݸ��ڵ�ͱ�ǩ��ȡ��ǩֵ
	 * @param parent  ���ڵ�
	 * @param tagName ��ǩ
	 * @return
	 */
	public static Object getElementValue(Element parentElement,String tagName){
		Object value = null;
		Element element = getChildElement(parentElement, tagName);
		NodeList nodes = element.getChildNodes();
		if(nodes != null && nodes.getLength()>0){
			value = nodes.item(0).getNodeValue();
		}
		return value;
	}
	
	/**
	 * 
	 * @param element
	 * @return
	 */
	public static Object getElementValue(Element element){
		Object value = null;
		NodeList nodes = element.getChildNodes();
		if(nodes != null && nodes.getLength()>0){
			value = nodes.item(0).getNodeValue();
		}
		return value;
	} 
	
	public static void main(String[] args) {
		String ddd="<OPHIUX>"
	    +"<HEAD>"
	    +"<SERVICE_NAME>own_fd_informationInfo</SERVICE_NAME>"
	    +"<PARAM>"
			+"<S_DATE></S_DATE><MODULE_NAME></MODULE_NAME><INFORMATION_TITLE></INFORMATION_TITLE><MODULE_CODE></MODULE_CODE><INFORMATION_ID></INFORMATION_ID>"	
		+"</PARAM>"
		+"<R_FORM>XML</R_FORM>"
		+"<ISSUCCESS>true</ISSUCCESS>"
		+"<HIS_MESSAGE></HIS_MESSAGE>"
		+"<RESULTCODE>100</RESULTCODE>"
		+"<MESS_COUNT>7</MESS_COUNT>"
	+"</HEAD>"
	+"<MESSAGE>"
				
				+"<DATA>"
							+"<MODULE_NAME>每日消息</MODULE_NAME>"
							+"<CREATE_TIME>2017-10-17 14:51:28</CREATE_TIME>"
							+"<INFORMATION_TITLE>重要通知</INFORMATION_TITLE>"
							+"<ID>8</ID>"
							+"<INFORMATION_PHOTO></INFORMATION_PHOTO>"
							+"<REMARK></REMARK>"
		+"<MODULE_CODE>MRXX</MODULE_CODE>"
							+"<UPDATE_TIME>2017-10-17 14:58:47</UPDATE_TIME>"
							+"<INFORMATION_CONTEXT><![CDATA[<h3><b>我们开始更新了！</b></h3><p>以后每日都会推送一条当日重要新闻！</p><p style=\"text-align: right; \">2017-10-17</p><ul class=\"edu_li clearfix\" style=\"margin-bottom: 0px; list-style: none; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, SimHei, Arial, SimSun; font-size: 16px;\"><li style=\"margin-bottom: 10px; padding-right: 20px; padding-left: 20px; display: inline-block;\"><div style=\"margin-right: 10px; position: relative; height: 58px; font-size: 12px; float: left;\"><a href=\"http://edu.csdn.net/huiyiCourse/detail/569?utm_source=blog9\" target=\"_blank\" style=\"color: rgb(202, 12, 22); margin: 0px; padding: 0px; outline: 0px; display: block; word-wrap: break-word; word-break: normal;\"></a></div><div style=\"position: relative; height: 58px; font-size: 12px; float: left; width: 160px;\"><p style=\"margin-bottom: 0px; font-size: 14px; line-height: 18px;\"><font color=\"#757a7e\"><span style=\"outline-color: initial; outline-width: initial; display: block;\">Python全栈工程师入门指南</span></font></p><p style=\"margin-bottom: 0px; position: absolute; bottom: 0px; left: 0px; color: rgb(119, 128, 135);\">讲师：</p></div></li><li style=\"margin-bottom: 10px; padding-right: 20px; padding-left: 20px; display: inline-block;\"><div style=\"margin-right: 10px; position: relative; height: 58px; font-size: 12px; float: left;\"><a href=\"http://edu.csdn.net/huiyiCourse/series_detail/68?utm_sorce=blog9\" target=\"_blank\" style=\"color: rgb(117, 122, 126); margin: 0px; padding: 0px; outline: none; display: block; word-wrap: break-word; word-break: normal;\"></a></div><div style=\"position: relative; height: 58px; font-size: 12px; float: left; width: 160px;\"><p style=\"margin-bottom: 0px; font-size: 14px; line-height: 18px;\"><font color=\"#757a7e\"><span style=\"outline-color: initial; outline-width: initial; display: block;\">自然语言处理在“天猫精灵”的实践应用</span></font></p><p style=\"margin-bottom: 0px; position: absolute; bottom: 0px; left: 0px; color: rgb(119, 128, 135);\">讲师：姜飞俊</p></div></li></ul>]]]]>></INFORMATION_CONTEXT>"
								+"</DATA>"
	    +"</MESSAGE>"
	+"</OPHIUX>";
		
		
		try {
			System.out.println(getDocmentElement(ddd));
		} catch (OphiuxWechatTemplateMessageException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
